package XML;

/**
 *
 * @author Bashev
 */
import excel.CSVFile;
import excel.ExcelFile;
import java.io.File;
import java.io.IOException;
import org.apache.commons.digester3.Digester;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.xml.sax.SAXException;

public class DigesterMarriesLucene
{
    private static IndexWriter writer; 

    public void addString(String resource) throws IOException
    {
       // System.out.println("Adding " + resource);
        Document contactDocument  = new Document();
        Field ff = new Field("content", resource, Field.Store.YES, Field.Index.ANALYZED);
        contactDocument.add(ff);       
        writer.addDocument(contactDocument);
    }

    public static IndexReader parse(File file, String outputName) throws IOException, SAXException
    {       
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);        
        Directory index = new RAMDirectory();
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, analyzer);
        writer = new IndexWriter(index, config);
        Digester digester = new Digester();
        digester.setValidating(false);
        digester.addObjectCreate("resources", DigesterMarriesLucene.class );
        digester.addCallMethod("resources/string", "addString", 0);  
        digester.parse(file);   
        writer.close();        
        IndexReader ir = IndexReader.open(index);   
        System.out.println(ir.document(1).get("content"));
        
        CSVFile csv = new CSVFile(ir); 
        csv.createCSVFile(outputName); 
        
        ExcelFile ef = new ExcelFile(ir);
        ef.createXLSFile(outputName);
        return ir;            
    }
        
}
